package com.work.mvc.view.table;

import java.util.List;

import com.vaadin.data.*;
import com.vaadin.data.util.IndexedContainer;
import com.work.db.entity.BaseEntity;
import com.work.mvc.view.table.property.AbstractTableProperty;

public class TableContentFactory {
	
	private static TableContentFactory tableFactory = null;

	protected TableContentFactory() {}

	public static TableContentFactory getInstance()
	{
		if (tableFactory == null) {
			tableFactory = new TableContentFactory();
		}
		return tableFactory;
	}
	
	@SuppressWarnings({"rawtypes", "unchecked"})
	public IndexedContainer createTableContent(List<? extends AbstractTableProperty> properties, List<? extends BaseEntity> dataList) {
		IndexedContainer container = new IndexedContainer();
		for (AbstractTableProperty containerProperty : properties) {
			container.addContainerProperty(containerProperty, containerProperty.getType(), null);
		}
		
		for (BaseEntity baseEntity : dataList) {
			Item item = container.addItem(baseEntity);
			for (AbstractTableProperty property : properties) {
				Object cellValue = property.getValue(baseEntity);
				Property cell = item.getItemProperty(property);
				cell.setValue(cellValue);
			}
		}
		
		return container;
	}
}
